home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / Fragment Tool / Sources / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  1995-11-20  |  30.4 KB  |  1,401 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.                   9/28/95    CW        First release
  13.  
  14. */
  15.  
  16. #ifndef __TYPES__
  17.     #include <Types.h>
  18. #endif
  19.  
  20. #ifndef __QUICKDRAW__
  21.     #include <Quickdraw.h>
  22. #endif
  23.  
  24. #ifndef __QDOFFSCREEN__
  25.     #include <QDOffscreen.h>
  26. #endif
  27.  
  28. #ifndef __DESK__
  29.     #include <Desk.h>
  30. #endif
  31.  
  32. #ifndef __LOWMEM__
  33.     #include <LowMem.h> /* For MemError() */
  34. #endif
  35.  
  36. #ifndef __SCRIPT__
  37.     #include <Script.h>
  38. #endif
  39.  
  40. #ifndef __TEXTUTILS__
  41.     #include <TextUtils.h>
  42. #endif
  43.  
  44. #ifndef __DIALOGS__
  45.     #include <Dialogs.h>
  46. #endif
  47.  
  48. #ifndef __FOLDERS__
  49.     #include <Folders.h>
  50. #endif
  51.  
  52. #ifndef __STDDEF__
  53.     #include <stddef.h>
  54. #endif
  55.  
  56.  
  57. #include <CodeFragments.h>
  58. #include <StandardFile.h>
  59. #include <Folders.h>
  60.  
  61. #include "FragmentTool.h"
  62. #include "FragmentStuff.h"
  63. #include "Dialogs.h"
  64. #include "Streams.h"
  65. #include "Utilities.h"
  66.  
  67. #include "Prototypes.h"
  68.  
  69.  
  70. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  71. static void AddContentsToList ( WindowRef theWindow );
  72. static void DoDialogItemHit ( DialogRef theDialog, int16 theItem );
  73. static int8 GetStageFromItem ( int16 theItem );
  74. static int16 GetItemFromStage ( int8 theStage );
  75.  
  76. static OSErr CreateTempAndCopyFragment ( tHeaderHan sourceHeader, FSSpecPtr sourceSpec, int16 sourceIndex,
  77.                                         tHeaderHan targetHeader );
  78.  
  79.  
  80.  
  81.  
  82. void DoNewDocument ( void )
  83. {
  84.     OSErr            theErr;
  85.     int16            theRef;
  86.     int16            theVol;
  87.     int32            theDir;
  88.     FSSpec            theSpec;
  89.     Str255            tmpStr = "\p";
  90.     Str255            theFile = "\pFragmentTool ";
  91.     hdrHand            theHeader;
  92.     Size            headerSize;
  93.     
  94.     
  95.     theErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &theVol, &theDir );
  96.     OSTypeToPStr ( TickCount ( ), tmpStr );
  97.     ConcatPStr ( theFile, tmpStr, sizeof ( Str255 ) );
  98.     theErr = FSMakeFSSpec ( theVol, theDir, theFile, &theSpec );
  99.     theErr = FSpCreate ( &theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  100.     
  101.  
  102.     // The file has been created with both forks, but it doesn't have a
  103.     // resource map yet. If we try to open it now, we'll get an eofErr (-39).
  104.     FSpCreateResFile ( &theSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  105.     theRef = FSpOpenResFile ( &theSpec, fsRdWrPerm );
  106.     if ( theRef == -1 )
  107.     {
  108.         theErr = ResError ( );
  109.         AlertUser ( kGenericErrorStr, theErr, nil );
  110.         return;
  111.     }
  112.     
  113.     headerSize = offsetof ( cfrgHeader, arrayStart );
  114.     theHeader = (hdrHand) NewHandleClear ( headerSize );
  115.     (*theHeader)->version = 1;        // Current version number
  116.     AddResource ( (Handle) theHeader, kCFragResourceType, kCFragResourceID, "\p" );
  117.     CloseResFile ( theRef );
  118.     
  119.     GetIndString ( tmpStr, 1000, 4 );                // Default document name
  120.     theErr = OpenDocument ( &theSpec, tmpStr );
  121.     if ( theErr )
  122.     {
  123.         AlertUser ( kGenericErrorStr, theErr, nil );
  124.         return;
  125.     }
  126.     
  127.     return;
  128.     
  129. }
  130.  
  131.  
  132.  
  133. void DoOpenDocument ( void )
  134. {
  135.     OSErr                theErr;
  136.     StandardFileReply    theReply;
  137.     
  138.     
  139.     StandardGetFile ( nil, -1, nil, &theReply );
  140.     if ( theReply.sfGood )
  141.     {
  142.         theErr = OpenDocument ( &theReply.sfFile, nil );
  143.         
  144.         if ( theErr )
  145.             AlertUser ( kGenericErrorStr, theErr, nil );
  146.     }
  147.     
  148.     return;
  149. }
  150.  
  151.  
  152.  
  153. OSErr OpenDocument ( FSSpecPtr fileSpec, StringPtr documentName )
  154. {
  155.     OSErr                theErr;
  156.     int16                theFileRef, saveFile;
  157.     WindowRef            theWindow;
  158.     tWindowInfoPtr        theInfo;
  159.     tAddFragmentsRec    theRec;
  160.     Str255                theTitle;
  161.     
  162.     if ( documentName )
  163.         CopyPStr ( documentName, theTitle, sizeof ( Str255 ) );
  164.     else
  165.         CopyPStr ( fileSpec->name, theTitle, sizeof ( Str255 ) );
  166.     
  167.     saveFile = CurResFile ( );
  168.     SetResLoad ( false );
  169.     theFileRef = FSpOpenResFile ( fileSpec, fsRdPerm );
  170.     SetResLoad ( true );
  171.     UseResFile ( saveFile );
  172.     if ( theFileRef == -1 )
  173.         return ResError ( );
  174.     
  175.     theRec.u.out.theFileRef = theFileRef;
  176.     theErr = CreateDocumentWindow ( &theWindow, theTitle, AddFragments, (void*) &theRec );
  177.     if ( theErr )
  178.         return theErr;
  179.     
  180.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  181.     theInfo->bUntitled = (documentName) ? true : false;
  182.     theInfo->dataHandle = theRec.u.in.theDataHandle;
  183.     BlockMoveData ( fileSpec, &theInfo->fileSpec, sizeof ( FSSpec ) );
  184.     
  185.     return theErr;
  186. }
  187.  
  188.  
  189.  
  190. OSErr CreateDocumentWindow ( WindowRef* windowRef, Str255 windowTitle,
  191.                                 tContentsProcPtr contentsProc, void* refCon )
  192. {
  193.     OSErr        theErr;
  194.     WindowRef    theWindow;
  195.     
  196.     
  197.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1L );
  198.     if ( theWindow == nil )
  199.         return (ResError ( )) ? ResError ( ) : resNotFound;
  200.     
  201.     theErr = CreateWindowInfo ( theWindow, sizeof ( tWindowInfo ) );
  202.     if ( theErr )    goto CleanupAndBail;
  203.     
  204.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  205.     if ( theErr )    goto CleanupAndBail;
  206.     
  207.     
  208.     // Since the application can function without the Drag Manager, an
  209.     // error here is not considered fatal.
  210.     theErr = InstallDragHandlers ( theWindow );
  211.     
  212.     SetWindowType ( theWindow, kDocumentWindowType );
  213.     SetPortWindowPort ( theWindow );
  214.     TextFont ( geneva );
  215.     TextFace ( normal );
  216.     TextSize ( 9 );
  217.     
  218.     SetWTitle ( theWindow, windowTitle );
  219.     SelectWindow ( theWindow );
  220.     ShowWindow ( theWindow );
  221.     
  222.     *windowRef = theWindow;
  223.     
  224.     return noErr;
  225.     
  226.  
  227. CleanupAndBail:
  228.     
  229.     // Don't forget to free any storage we've used so far
  230.     DestroyDocumentWindow ( theWindow );
  231.     
  232.     return theErr;
  233. }
  234.  
  235.  
  236.  
  237. //
  238. // This will close the document's file, dispose of any storage we've hung
  239. // off the window, remove any drag handlers, and then dispose of the window.
  240. //
  241. WindowRef DestroyDocumentWindow ( WindowRef windowRef )
  242. {
  243.     if ( windowRef )
  244.     {
  245.         tWindowInfoPtr theInfo;
  246.         
  247.         theInfo = (tWindowInfoPtr) GetWRefCon ( windowRef );
  248.         if ( theInfo )
  249.         {
  250.             if ( theInfo->listRef )
  251.                 LDispose ( theInfo->listRef );
  252.             
  253.             if ( theInfo->dataHandle )
  254.                 DisposeHandle ( theInfo->dataHandle );
  255.  
  256.             DisposePtr ( (Ptr) theInfo );
  257.         }
  258.         
  259.         RemoveDragHandlers ( windowRef );
  260.         DisposeWindow ( windowRef );
  261.         
  262.     }
  263.     
  264.     // Return nil so it can be assigned to the window
  265.     // reference which was passed in
  266.     return nil;
  267. }
  268.  
  269.  
  270.  
  271. ////////////////////////////////////////////////////////////////////////////////////
  272.  
  273.  
  274.  
  275. //
  276. // Draw *only* the grow icon
  277. //
  278. void DrawClippedGrowIcon ( WindowRef theWindow )
  279. {
  280.     Rect        portRect = theWindow->portRect;
  281.     RgnHandle    oldClip = NULL;
  282.     Rect        newClip;
  283.     
  284.     
  285.     SetPort ( theWindow );
  286.     oldClip = NewRgn ( );
  287.     GetClip ( oldClip );
  288.     
  289.     newClip = portRect;
  290.     newClip.top = newClip.bottom - 15;
  291.     newClip.left = newClip.right - 15;
  292.     ClipRect ( &newClip );
  293.     DrawGrowIcon ( theWindow );
  294.     
  295.     SetClip ( oldClip );
  296.     DisposeRgn ( oldClip );
  297.     
  298.     return;
  299. }
  300.  
  301.  
  302.  
  303.  
  304. //
  305. // This is passed into the CreateDocumentWindow routine
  306. // to add the contents to the newly created list.
  307. //
  308. OSErr AddFragments ( ListRef theList, void* refCon )
  309. {
  310.     OSErr                theErr;
  311.     int16                theFileRef, saveFile;
  312.     int                    index, itemCount;
  313.     Handle                resourceScratch;
  314.     tHeaderHan            parsedResource;
  315.     tAddFragmentsRec*    theRec;
  316.     
  317.     
  318.     
  319.     
  320.     theRec = (tAddFragmentsRec*) refCon;
  321.     theFileRef = theRec->u.out.theFileRef;
  322.     
  323.     parsedResource = (tHeaderHan) NewHandleClear ( sizeof ( tHeader ) );
  324.     theErr = MemError ( );
  325.     if ( theErr )
  326.         return theErr;
  327.     
  328.     saveFile = CurResFile ( );
  329.     UseResFile ( theFileRef );
  330.     resourceScratch = Get1Resource ( kCFragResourceType, kCFragResourceID );
  331.     theErr = ResError ( );
  332.     if ( theErr || resourceScratch == nil)
  333.         return (theErr) ? theErr : resNotFound;
  334.     UseResFile ( saveFile );
  335.     
  336.     
  337.     if ( parsedResource )
  338.     {
  339.         ParseResource ( resourceScratch, parsedResource );
  340.         
  341.         itemCount = (*parsedResource)->itemCount;
  342.         for ( index = 0; index < itemCount; index++ )
  343.         {
  344.             tItemPtr     dstItem;
  345.             
  346.             dstItem = &(*parsedResource)->itemList[index];
  347.             AddFragToList ( theList, dstItem );
  348.             
  349.         }
  350.         
  351.         
  352.         theRec->u.in.theDataHandle = (Handle) parsedResource;
  353.     }
  354.     
  355.     ReleaseResource ( resourceScratch );
  356.         
  357.     return noErr;
  358.     
  359. } // AddFragments
  360.  
  361.  
  362.  
  363. //
  364. // This returns the Nth document window, ignoring the front-most
  365. //
  366. WindowRef GetIndexedDocumentWindow ( int theIndex )
  367. {
  368.     Boolean        bFirst = true;
  369.     int            thisIndex = 0;        // Must be zero-based, like the List Manager
  370.     WindowRef    currWindow;
  371.     
  372.     
  373.     currWindow = FrontWindow ( );
  374.     while ( currWindow )
  375.     {
  376.         if ( GetWindowType ( currWindow ) == kDocumentWindowType )
  377.         {
  378.             // Skip the front-most document since that's the source document
  379.             if ( bFirst )
  380.                 bFirst = false;
  381.             else
  382.             {
  383.                 if ( thisIndex == theIndex )
  384.                     return currWindow;
  385.                 
  386.                 thisIndex++;
  387.             }
  388.         }
  389.         
  390.         currWindow = GetNextWindow ( currWindow );
  391.     }
  392.     
  393.     return nil;
  394. }
  395.  
  396.  
  397.  
  398. //
  399. // Creates the storage for the data to hang off a window or dialog
  400. //
  401. OSErr CreateWindowInfo ( WindowRef windowRef, Size infoSize )
  402. {
  403.     OSErr    theErr;
  404.     Ptr        theInfo = nil;
  405.     
  406.     
  407.     theInfo = NewPtrClear ( infoSize );
  408.     theErr = MemError ( );
  409.     if ( theErr )
  410.         return theErr;
  411.     
  412.     SetWRefCon ( windowRef, (long) theInfo );
  413.     
  414.     return noErr;
  415. }
  416.  
  417.  
  418.  
  419. //
  420. // Closes all the open documents. Called just before a quit, or as
  421. // the result of an option click in a windows go away box.
  422. //
  423. Boolean DoCloseAllDocuments ( void )
  424. {
  425.     Boolean        bCancelled = false;
  426.     WindowRef    aWindow;
  427.     
  428.     aWindow = FrontWindow ( );
  429.     while ( aWindow && !bCancelled )
  430.     {
  431.         bCancelled = DoCloseDocument ( aWindow );
  432.         aWindow = GetNextWindow ( aWindow );
  433.     }
  434.     
  435.     return bCancelled;
  436. }
  437.  
  438.  
  439.  
  440. Boolean DoCloseDocument ( WindowRef theWindow )
  441. {
  442.     int16    windowType;
  443.     
  444.     if ( theWindow )
  445.     {
  446.         windowType = GetWindowType ( theWindow );
  447.         if ( windowType == kDAWindowType )
  448.             CloseDeskAcc ( GetWindowKind ( theWindow ) );
  449.         else if ( windowType == kDocumentWindowType )
  450.         {
  451.             int                i;
  452.             tWindowInfoPtr    theInfo;
  453.             tHeaderHan        theHeader;
  454.             tItemPtr        anItem;
  455.             
  456.             
  457.             // Check for any fragments that have temp files
  458.             theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  459.             if ( theInfo->bDirty )
  460.                 if ( DoSaveBeforeClosing ( theWindow ) ) // Did the user cancel?
  461.                     return kCancelled;
  462.                     
  463.             theHeader = (tHeaderHan) theInfo->dataHandle;
  464.             HLock ( (Handle) theHeader );
  465.             for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  466.             {
  467.                 anItem = GetNthItem ( theHeader, i );
  468.                 if ( anItem == nil )
  469.                     break;
  470.                 
  471.                 DecrementTempUsageCount ( anItem );
  472.             }
  473.             HUnlock ( (Handle) theHeader );
  474.  
  475.             DestroyDocumentWindow ( theWindow );
  476.         }    
  477.         else if ( windowType == kListWindowType )
  478.             DestroyDialog ( theWindow );
  479.             
  480.         AdjustMenus ( );
  481.     }
  482.     
  483.     return kNotCancelled;
  484.     
  485. } // CloseDocument
  486.  
  487.  
  488.  
  489. Boolean DoSaveBeforeClosing ( WindowRef theWindow )
  490. {
  491.     int16        theItem;
  492.     DialogRef    theDialog;
  493.     WindowRef    dialogWindow;
  494.     Str255        theTitle, theReason;
  495.     
  496.     
  497.     theDialog = GetNewDialog ( kSaveDontsaveDialog, nil, (WindowRef) -1 );
  498.     dialogWindow = GetDialogWindow ( theDialog );
  499.     SetPortWindowPort ( dialogWindow );
  500.     
  501.     GetWTitle ( theWindow, theTitle );
  502.     if ( gQuit )
  503.         GetIndString ( theReason, 1000, 6 );
  504.     else
  505.         GetIndString ( theReason, 1000, 7 );
  506.     ParamText ( theTitle, theReason, nil, nil );
  507.     ShowWindow ( dialogWindow );
  508.     SelectWindow ( dialogWindow );
  509.     
  510.     SetDialogDefaultItem ( theDialog, kStdOkItemIndex );
  511.     SetDialogCancelItem ( theDialog, kStdCancelItemIndex );
  512.     
  513.     
  514.     // As long as the only enabled items are the dimissers, 
  515.     // there is no need to call ModalDialog within a loop.
  516.     ModalDialog ( nil, &theItem );
  517.     DisposeDialog ( theDialog );
  518.     
  519.     if ( theItem == kStdOkItemIndex )
  520.         return DoSave ( theWindow );
  521.     
  522.     return theItem == kStdCancelItemIndex;
  523. }        
  524.  
  525.  
  526.  
  527. Boolean DoSave ( WindowRef theWindow )
  528. {
  529.     int16            saveFile, theFileRef;
  530.     int                i;
  531.     OSErr            theErr;
  532.     Handle            resourceScratch;
  533.     tWindowInfoPtr    theInfo;
  534.     tHeaderHan        theHeader;
  535.     tItemPtr        anItem;
  536.     
  537.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  538.     #if DEBUGGING
  539.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  540.     #endif
  541.     
  542.     if ( theInfo->bUntitled )
  543.         return DoSaveAs ( theWindow );
  544.     
  545.     // Check for any fragments that have been deleted...
  546.     theHeader = (tHeaderHan) theInfo->dataHandle;
  547.     HLock ( (Handle) theHeader );
  548.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  549.     {
  550.         anItem = GetNthItem ( theHeader, i );
  551.         if ( anItem->bDeleted )
  552.         {
  553.             if ( anItem->bExistsInDocument )
  554.             {
  555.                 theErr = DeleteFragment ( theHeader, &theInfo->fileSpec, i );
  556.                 if ( theErr )
  557.                 {
  558.                     AlertUser ( kGenericErrorStr, theErr, nil );
  559.                     return kNotCancelled;
  560.                 }
  561.                 anItem->bExistsInDocument = false;
  562.             }
  563.             
  564.             DecrementTempUsageCount ( anItem );
  565.         }
  566.     }
  567.     
  568.     // ...and for any new fragments
  569.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  570.     {
  571.         anItem = GetNthItem ( theHeader, i );
  572.         
  573.         if ( anItem->bDeleted || anItem->bExistsInDocument )
  574.             continue;
  575.             
  576.         anItem->codeOffset = 0L;
  577.         theErr = AppendFileData ( GetTempSpecPtr ( anItem ), &theInfo->fileSpec, &anItem->codeOffset, &anItem->codeLength );
  578.         if ( theErr )
  579.         {
  580.             AlertUser ( kGenericErrorStr, theErr, nil );
  581.             return kNotCancelled;
  582.         }
  583.         
  584.         anItem->bExistsInDocument = true;
  585.     }
  586.     
  587.     HUnlock ( (Handle) theHeader );
  588.     
  589.     
  590.     saveFile = CurResFile ( );
  591.     theFileRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  592.     theErr = ResError ( );
  593.     if ( theErr )
  594.     {
  595.         int16    theIndex;
  596.         
  597.         theIndex = (theErr == opWrErr || theErr == permErr) ? kFileOpenStr : kGenericErrorStr;
  598.         AlertUser ( theIndex, (theErr == noErr) ? resNotFound : theErr, nil );
  599.         return kNotCancelled;
  600.     }
  601.     UseResFile ( theFileRef );
  602.     
  603.     resourceScratch = Get1Resource ( kCFragResourceType, kCFragResourceID );
  604.     theErr = ResError ( );
  605.     if ( theErr || resourceScratch == nil )
  606.     {
  607.         AlertUser ( kGenericErrorStr, (theErr == noErr) ? resNotFound : theErr, nil );
  608.         return kNotCancelled;
  609.     }
  610.     
  611.     theErr = BuildResource ( (tHeaderHan) theInfo->dataHandle, resourceScratch );
  612.     if ( theErr )
  613.     {
  614.         AlertUser ( kGenericErrorStr, theErr, nil );
  615.         return kNotCancelled;
  616.     }
  617.     
  618.     ChangedResource ( resourceScratch );
  619.     UpdateResFile ( theFileRef );
  620.     ReleaseResource ( resourceScratch );
  621.     CloseResFile ( theFileRef );
  622.     UseResFile ( saveFile );
  623.     
  624.     SetDocumentDirty ( theWindow, false );
  625.     
  626.     return kNotCancelled;
  627. }
  628.  
  629.  
  630.  
  631. Boolean DoSaveAs ( WindowRef theWindow )
  632. {
  633.     tWindowInfoPtr        theInfo;
  634.     StandardFileReply    theReply;
  635.     Str255                defaultName;
  636.     
  637.     
  638.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  639.     #if DEBUGGING
  640.     if ( theInfo == nil ) DebugStr ( "\p theInfo == nil" );
  641.     #endif
  642.     
  643.     GetWTitle ( theWindow, defaultName );
  644.     StandardPutFile ( "\p", defaultName, &theReply );
  645.     if ( theReply.sfGood )
  646.     {
  647.         OSErr    theErr;
  648.         
  649.         if ( theInfo->bUntitled )
  650.         {
  651.             theInfo->bUntitled = false;
  652.             DoSave ( theWindow );
  653.             
  654.             theErr = FSpCreate ( &theReply.sfFile, kFourQuestionMarks, kCFragLibraryFileType, theReply.sfScript );
  655.             theErr = FSpExchangeFiles ( &theInfo->fileSpec, &theReply.sfFile );
  656.             FSpDelete ( &theInfo->fileSpec );
  657.             
  658.             BlockMoveData ( &theReply.sfFile, &theInfo->fileSpec, sizeof ( FSSpec ) );
  659.             
  660.             SetWTitle ( theWindow, theReply.sfFile.name );
  661.         }
  662.         else        // User selected “Save As…”
  663.         {
  664.             int16        theRef, saveFile;
  665.             int            i;
  666.             FSSpec        oldSpec;
  667.             tItemPtr    anItem;
  668.             hdrHand        theResource = nil;
  669.             tHeaderHan    theHeader;
  670.             
  671.             
  672.             BlockMoveData ( &theInfo->fileSpec, &oldSpec, sizeof ( FSSpec ) );
  673.             BlockMoveData ( &theReply.sfFile, &theInfo->fileSpec, sizeof ( FSSpec ) );
  674.             theErr = FSpCreate ( &theInfo->fileSpec, kFourQuestionMarks, kCFragLibraryFileType, theReply.sfScript );
  675.             
  676.             theHeader = (tHeaderHan) theInfo->dataHandle;
  677.             HLock ( (Handle) theHeader );
  678.             // copy each fragment's data fork
  679.             for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  680.             {
  681.                 FSSpecPtr    theSpecPtr;
  682.  
  683.                 anItem = GetNthItem ( theHeader, i );
  684.                 if ( anItem->bDeleted )
  685.                     continue;
  686.                 
  687.                 if ( anItem->bExistsInDocument )
  688.                     theSpecPtr = &oldSpec;
  689.                 else
  690.                 {
  691.                     theSpecPtr = GetTempSpecPtr ( anItem );
  692.                     anItem->codeOffset = 0L;
  693.                 }
  694.                     
  695.                 theErr = AppendFileData ( theSpecPtr, &theInfo->fileSpec, &anItem->codeOffset, &anItem->codeLength );
  696.                 if ( theErr )
  697.                 {
  698.                     AlertUser ( kGenericErrorStr, theErr, nil );
  699.                     return kNotCancelled;
  700.                 }
  701.                 
  702.                 anItem->bExistsInDocument = true;
  703.             }
  704.             HUnlock ( (Handle) theHeader );
  705.             
  706.             
  707.             FSpCreateResFile ( &theInfo->fileSpec, kFourQuestionMarks, kCFragLibraryFileType, smSystemScript );
  708.             theRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  709.             
  710.             theResource = (hdrHand) NewHandleClear ( offsetof ( cfrgHeader, arrayStart ) );
  711.             (*theResource)->version = 1;        // Current version number
  712.             
  713.             theErr = BuildResource ( (tHeaderHan) theInfo->dataHandle, (Handle) theResource );
  714.             
  715.             saveFile = CurResFile ( );
  716.             theRef = FSpOpenResFile ( &theInfo->fileSpec, fsRdWrPerm );
  717.             // If the file is already open, it may not be the current resource file
  718.             UseResFile ( theRef );
  719.             AddResource ( (Handle) theResource, kCFragResourceType, kCFragResourceID, "\p" );
  720.             UpdateResFile ( theRef );
  721.             ReleaseResource ( (Handle) theResource );
  722.             CloseResFile ( theRef );
  723.             UseResFile ( saveFile );
  724.             
  725.             
  726.             SetWTitle ( theWindow, theReply.sfFile.name );
  727.             
  728.             SetDocumentDirty ( theWindow, false );
  729.         }
  730.         
  731.         return kNotCancelled;
  732.     }
  733.     
  734.     return kCancelled;
  735. }
  736.  
  737.  
  738.  
  739. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  740. {
  741.     OSErr        theErr;
  742.     ListHandle    theList;
  743.     Point        cellSize = {0, 32767};
  744.     Cell        firstCell = {0, 0};
  745.     Rect         dataRect = {0, 0, 0, 1};
  746.     Rect        viewRect;
  747.     
  748.     
  749.     // The width of a cell needs to be 32767 so any hiliting
  750.     // doesn't stop short of the window width after it's been resized.
  751.     
  752.     viewRect = theWindow->portRect;
  753.     viewRect.bottom -= 15; viewRect.right -= 15;
  754.     theList = LNew ( &viewRect, &dataRect, cellSize, 0, theWindow, 
  755.                         false, false, true, true );
  756.     if ( theList )
  757.     {
  758.         (*theList)->selFlags = lOnlyOne;
  759.         (*theList)->lClickLoop = gClickLoopUPP;
  760.         SetWListRef ( theWindow, theList );
  761.         
  762.         // Since the calling routine is always in the same architecture type as
  763.         // the callback routine, we don't need to worry about any Mixed Mode
  764.         // complications. We just treat it as a straight forward routine pointer.
  765.         if ( contentsProc )
  766.         {
  767.             theErr = (*contentsProc) ( theList, refCon );
  768.             if ( theErr )
  769.                 return theErr;
  770.         }
  771.         
  772.         LSetSelect ( true, firstCell, theList );
  773.         
  774.         // Now the list has been fully prepared, turn the drawing mode on
  775.         LSetDrawingMode ( true, theList );
  776.     }
  777.     
  778.     return noErr;
  779.     
  780. } // CreateContentList
  781.  
  782.  
  783.  
  784. Boolean IsDocumentDirty ( WindowRef theWindow )
  785. {
  786.     if ( GetWindowType ( theWindow ) == kDocumentWindowType )
  787.     {
  788.         tWindowInfoPtr    theInfo;
  789.         
  790.         theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  791.         if ( theInfo )
  792.             return ( theInfo->bDirty );
  793.     }
  794.     
  795.     return false;
  796. }
  797.  
  798.  
  799.  
  800. void SetDocumentDirty ( WindowRef theWindow, Boolean bIsDirty )
  801. {
  802.     if ( GetWindowType ( theWindow ) == kDocumentWindowType )
  803.     {
  804.         tWindowInfoPtr    theInfo;
  805.         
  806.         theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  807.         if ( theInfo )
  808.             theInfo->bDirty = bIsDirty;
  809.             
  810.         AdjustMenus ( );
  811.     }
  812.     
  813.     return;
  814. }
  815.  
  816.  
  817.  
  818. void SetWListRef ( WindowRef theWindow, ListHandle theList )
  819. {
  820.     ((tWindowInfoPtr) GetWRefCon ( theWindow ))->listRef = theList;
  821.     
  822.     return;
  823. }
  824.  
  825.  
  826.  
  827. ListHandle GetWListRef ( WindowRef theWindow )
  828. {
  829.     return ((tWindowInfoPtr) GetWRefCon ( theWindow ))->listRef;
  830. }
  831.  
  832.  
  833.  
  834. void SetWFileSpec ( WindowRef theWindow, FSSpecPtr theSpec )
  835. {
  836.     BlockMoveData ( theSpec, &((tWindowInfoPtr) GetWRefCon ( theWindow ))->fileSpec, sizeof ( FSSpec ) );
  837.     
  838.     return;
  839. }
  840.  
  841.  
  842.  
  843. /*----------------------- Handle window update events ----------------------*/
  844.  
  845. void DoUpdate ( EventRecord* theEvent )
  846. {
  847.     WindowRef    theWindow;
  848.     SInt16        theType;
  849.     GrafPtr        savePort;
  850.     
  851.     
  852.     
  853.     theWindow = (WindowRef) theEvent->message;
  854.             
  855.     
  856.     theType = GetWindowType ( theWindow );
  857.     GetPort ( &savePort );
  858.     SetPortWindowPort ( theWindow );
  859.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  860.     EraseRect ( &theWindow->portRect );
  861.  
  862.     switch ( theType )
  863.     {
  864.         case kDocumentWindowType:
  865.         {
  866.             ListHandle    theList = nil;
  867.             
  868.             theList = GetWListRef ( theWindow );
  869.             if ( theList )
  870.                 LUpdate ( (*theList)->port->visRgn, theList );
  871.             DrawClippedGrowIcon ( theWindow );
  872.         }
  873.         break;
  874.         
  875.         case kListWindowType:
  876.         {
  877.             ListHandle        theList = nil;
  878.             tDialogInfoPtr    theInfo;
  879.             
  880.             UpdateDialog ( theWindow, theWindow->visRgn );
  881.             theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  882.             theList = theInfo->listRef;
  883.             if ( theList )
  884.                 LUpdate ( (*theList)->port->visRgn, theList );
  885.         }
  886.         break;
  887.  
  888.         case kGetInfoWindowType:
  889.         {
  890.             TEHandle        textH;
  891.             
  892.             UpdateDialog ( theWindow, theWindow->visRgn );
  893.             textH = ((DialogPeek) theWindow)->textH;
  894.             if ( textH )
  895.                 TEUpdate ( &(*textH)->viewRect, textH );
  896.         }
  897.         break;
  898.     }
  899.     
  900.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  901.     SetPort ( savePort );
  902.     
  903.     return;
  904.     
  905. } // DoUpdate
  906.  
  907.  
  908.  
  909. void DoActivate ( EventRecord* theEvent )
  910. {
  911.     SInt16        itemHit;
  912.     SInt16        theType;
  913.     ListHandle    theList = nil;
  914.     WindowRef    theWindow = (WindowRef) theEvent->message;
  915.     Boolean        bActiveFlag = theEvent->modifiers & 1;
  916.     
  917.     
  918.     
  919.     theType = GetWindowType ( theWindow );
  920.     switch ( theType )
  921.     {
  922.         case kDocumentWindowType:
  923.         {
  924.             LActivate ( bActiveFlag, GetWListRef ( theWindow ) );
  925.             DrawClippedGrowIcon ( theWindow );
  926.         }
  927.         break;
  928.         
  929.         case kListWindowType:
  930.         {
  931.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  932.             theList = theInfo->listRef;
  933.             if ( theList )
  934.                 LActivate ( bActiveFlag, theList );
  935.             DialogSelect ( theEvent, &theWindow, &itemHit );
  936.         }
  937.         break;
  938.         
  939.         case kGetInfoWindowType:
  940.         {
  941.             TEHandle        textH;
  942.             
  943.             textH = ((DialogPeek) theWindow)->textH;
  944.             if ( textH )
  945.             {
  946.                 if ( bActiveFlag )
  947.                     TEActivate ( textH );
  948.                 else
  949.                     TEDeactivate ( textH );
  950.             }
  951.             DialogSelect ( theEvent, &theWindow, &itemHit );
  952.         }
  953.         break;
  954.     }
  955.     
  956.     
  957.     AdjustMenus ( );
  958.     
  959. } // DoActivate
  960.  
  961.  
  962.  
  963. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  964. {
  965.     WindowRef    frontWindow;
  966.     
  967.     // If a movable modal is active, ignore click in an inactive 
  968.     // window, otherwise select it or handle the content click.
  969.     
  970.     frontWindow = FrontWindow ( );
  971.     if ( theWindow != frontWindow )
  972.     {
  973.         if ( IsMovableModal ( frontWindow ) )
  974.             SysBeep ( 30 );
  975.         else
  976.             SelectWindow ( theWindow );
  977.     }
  978.     else
  979.     {
  980.         if ( IsMovableModal ( theWindow ) )
  981.             DoDialogContentClick ( theWindow, theEvent );
  982.         else
  983.         {
  984.             Boolean    bWasHandled;
  985.             bWasHandled = HandleListClick ( theWindow, theEvent );
  986.             
  987.             AdjustMenus ( );
  988.         }
  989.     }
  990.     
  991.     return;
  992.     
  993. } // DoContentClick
  994.  
  995.  
  996.  
  997.  
  998.  
  999. void DoGrowWindow ( WindowRef theWindow, EventRecord* theEvent )
  1000. {
  1001.     const short kMinDocSize = 64;
  1002.     const short kMaxDocSize = 32767;
  1003.     
  1004.     long    newSize;
  1005.     Rect    limitRect;
  1006.     
  1007.     
  1008.     // set up the limiting rectangle
  1009.     SetRect ( &limitRect, kMinDocSize, kMinDocSize, kMaxDocSize, kMaxDocSize );
  1010.     // call Window Manager to let the user drag size box
  1011.     newSize = GrowWindow ( theWindow, theEvent->where, &limitRect );
  1012.     if ( newSize )                    // if the user changed its size
  1013.     {
  1014.         short h, v;
  1015.         
  1016.         h = newSize & 0x0000FFFF;
  1017.         v = newSize >> 16;
  1018.         SizeWindow ( theWindow, h, v, true );
  1019.         h -= 15; v -= 15;
  1020.         LSize ( h, v, GetWListRef ( theWindow ) );
  1021.         SetPort ( theWindow );
  1022.         InvalRect ( &theWindow->portRect );
  1023.     }
  1024.     
  1025.     return;
  1026.     
  1027. } // DoGrowWindow
  1028.  
  1029.  
  1030.  
  1031.  
  1032. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  1033. {
  1034.     WindowRef    frontWindow;
  1035.     
  1036.     
  1037.     // If a movable modal is active, ignore click in an inactive 
  1038.     // title bar, otherwise let the Window Manager handle it.
  1039.     
  1040.     frontWindow = FrontWindow ( );
  1041.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  1042.         SysBeep ( 30 );
  1043.     else                                
  1044.     {
  1045.         RgnHandle    theRgn;
  1046.         Rect        dragRect;
  1047.         
  1048.         theRgn = GetGrayRgn ( );
  1049.         dragRect = (*theRgn)->rgnBBox;
  1050.         DragWindow ( theWindow, theEvent->where, &dragRect );
  1051.     }
  1052.     
  1053.     return;
  1054. }
  1055.  
  1056.  
  1057.  
  1058. Boolean IsMovableModal ( WindowRef theWindow )
  1059. {
  1060.     return (GetWVariant ( theWindow ) == movableDBoxProc);
  1061. }
  1062.  
  1063.  
  1064.  
  1065. int16 GetWindowType ( WindowRef theWindow )
  1066. {
  1067.     int16 theType = 0;
  1068.     
  1069.     if ( theWindow )
  1070.     {
  1071.         theType = GetWindowKind ( theWindow );
  1072.         if ( theType < 0 )
  1073.             theType = kDAWindowType;
  1074.         else if ( theType == kApplicationWindowKind )
  1075.             theType = kDocumentWindowType;
  1076.         else
  1077.         {
  1078.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1079.             if ( theInfo )
  1080.                 theType = theInfo->windowType;
  1081.         }
  1082.     }
  1083.     
  1084.     return theType;
  1085. }
  1086.  
  1087.  
  1088.  
  1089. void SetWindowType ( WindowRef theWindow, int16 theType )
  1090. {
  1091.     int16 theKind;
  1092.  
  1093.     if ( theWindow )
  1094.     {
  1095.         theKind = GetWindowKind ( theWindow );
  1096.         if ( theKind != kApplicationWindowKind )
  1097.         {
  1098.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1099.             if ( theInfo )
  1100.                 theInfo->windowType = theType;
  1101.         }
  1102.     }
  1103.     
  1104.     return;
  1105. }
  1106.  
  1107.  
  1108.  
  1109. int16 GetWindowSubType ( WindowRef theWindow )
  1110. {
  1111.     int16 theKind;
  1112.     int16 theType = 0;
  1113.     
  1114.     if ( theWindow )
  1115.     {
  1116.         theKind = GetWindowKind ( theWindow );
  1117.         if ( theKind != kApplicationWindowKind )
  1118.         {
  1119.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1120.             if ( theInfo )
  1121.                 theType = theInfo->windowSubType;
  1122.         }
  1123.     }
  1124.     
  1125.     return theType;
  1126. }
  1127.  
  1128.  
  1129.  
  1130. void SetWindowSubType ( WindowRef theWindow, int16 theType )
  1131. {
  1132.     int16 theKind;
  1133.  
  1134.     if ( theWindow )
  1135.     {
  1136.         theKind = GetWindowKind ( theWindow );
  1137.         if ( theKind != kApplicationWindowKind )
  1138.         {
  1139.             tDialogInfoPtr theInfo = (tDialogInfoPtr) GetWRefCon ( theWindow );
  1140.             if ( theInfo )
  1141.                 theInfo->windowSubType = theType;
  1142.         }
  1143.     }
  1144.     
  1145.     return;
  1146. }
  1147.  
  1148.  
  1149.  
  1150. void DoZoomWindow ( WindowRef theWindow, EventRecord* theEvent, int16 windowPart )
  1151. {
  1152.     Point                globalPt = theEvent->where;
  1153.     
  1154.     SetPort ( theWindow );
  1155.     if ( TrackBox ( theWindow, globalPt, windowPart ) )
  1156.     {
  1157.         /*
  1158.             TO DO:
  1159.                 This doesn't handle multiple screens
  1160.         */
  1161.         
  1162.         ZoomWindow ( theWindow, windowPart, true );
  1163.         InvalRect ( &theWindow->portRect );
  1164.     }
  1165.     
  1166.     return;
  1167. }
  1168.  
  1169.  
  1170.  
  1171. OSErr CopyWindowFragment ( WindowRef source, int16 sourceIndex, WindowRef target )
  1172. {
  1173.     int16            itemIndex;
  1174.     OSErr            theErr;
  1175.     tWindowInfoPtr    sourceInfo = nil;
  1176.     tWindowInfoPtr    targetInfo = nil;
  1177.     
  1178.     
  1179.     
  1180.     itemIndex = GetIndexFromNthWindowItem ( source, sourceIndex );
  1181.     
  1182.     sourceInfo = (tWindowInfoPtr) GetWRefCon ( source );
  1183.     targetInfo = (tWindowInfoPtr) GetWRefCon ( target );
  1184.     
  1185.     theErr = CreateTempAndCopyFragment ( (tHeaderHan) sourceInfo->dataHandle, &sourceInfo->fileSpec,
  1186.                                             itemIndex, (tHeaderHan) targetInfo->dataHandle );
  1187.     if ( theErr == noErr )
  1188.     {
  1189.         SignedByte    theState;
  1190.         tItemPtr    theItem;
  1191.         
  1192.         theState = HGetState ( sourceInfo->dataHandle );
  1193.         HLock ( sourceInfo->dataHandle );
  1194.         theItem = GetNthItem ( (tHeaderHan) sourceInfo->dataHandle, itemIndex );
  1195.         AddFragToList ( GetWListRef ( target ), theItem );
  1196.         HSetState ( sourceInfo->dataHandle, theState );
  1197.         
  1198.         SetDocumentDirty ( target, true );
  1199.     }
  1200.     
  1201.     return theErr;
  1202. }
  1203.  
  1204.  
  1205.  
  1206. OSErr MoveWindowFragment ( WindowRef source, int16 sourceIndex, WindowRef target )
  1207. {
  1208.     OSErr    theErr = noErr;
  1209.     
  1210.     theErr = CopyWindowFragment ( source, sourceIndex, target );
  1211.     if ( !theErr )
  1212.         theErr = DeleteWindowFragment ( source, sourceIndex );
  1213.     
  1214.     return theErr;
  1215. }
  1216.  
  1217.  
  1218.  
  1219. OSErr DeleteWindowFragment ( WindowRef theWindow, int16 theIndex )
  1220. {
  1221.     int16            itemIndex;
  1222.     OSErr            theErr;
  1223.     tWindowInfoPtr    theInfo;
  1224.     
  1225.     
  1226.     // Since we don't support undo just yet, there's no need
  1227.     // to copy it to a temporary file.
  1228.     
  1229.     itemIndex = GetIndexFromNthWindowItem ( theWindow, theIndex );
  1230.     
  1231.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1232.     theErr = DeleteFragment ( (tHeaderHan) theInfo->dataHandle, nil, itemIndex );
  1233.     
  1234.     if ( theErr == noErr )
  1235.     {
  1236.         DeleteFromList ( GetWListRef ( theWindow ), theIndex );
  1237.         SetDocumentDirty ( theWindow, true );
  1238.     }
  1239.     
  1240.     return theErr;
  1241. }
  1242.  
  1243.  
  1244.  
  1245. OSErr CreateTempAndCopyFragment ( tHeaderHan sourceHeader, FSSpecPtr sourceSpec, int16 sourceIndex,
  1246.                                         tHeaderHan targetHeader )
  1247. {
  1248.     SignedByte    sourceState, targetState;
  1249.     OSErr        theErr;
  1250.     tItemPtr    sourceItem, targetItem;
  1251.     FSSpec        tempSpec;
  1252.     
  1253.     
  1254.     sourceState = HGetState ( (Handle) sourceHeader );
  1255.     HLock ( (Handle) sourceHeader );
  1256.     targetState = HGetState ( (Handle) targetHeader );
  1257.     HLock ( (Handle) targetHeader );
  1258.     
  1259.     sourceItem = GetNthItem ( sourceHeader, sourceIndex );
  1260.     // Do we already have a temporary file that contains this fragment
  1261.     if ( GetTempUsageCount ( sourceItem ) )
  1262.     {
  1263.         theErr = CopyFragment ( sourceHeader, nil, sourceIndex, targetHeader, nil );
  1264.         
  1265.         targetItem = GetLastItem ( targetHeader );
  1266.         targetItem->bExistsInDocument = false;
  1267.         IncrementTempUsageCount ( targetItem );
  1268.     }
  1269.     else
  1270.     {
  1271.         theErr = CreateTemporaryFile ( &tempSpec );
  1272.         
  1273.         theErr = CopyFragment ( sourceHeader, sourceSpec, sourceIndex, targetHeader, &tempSpec );
  1274.         if ( theErr )
  1275.             // We'll let the system handle the temp file. It may
  1276.             // be useful for debugging, if nothing else.
  1277.             return theErr;
  1278.         
  1279.         sourceItem = GetNthItem ( sourceHeader, sourceIndex );
  1280.         IncrementTempUsageCount ( sourceItem );
  1281.         BlockMoveData ( &tempSpec, GetTempSpecPtr ( sourceItem ), sizeof ( FSSpec ) );
  1282.         
  1283.         targetItem = GetLastItem ( targetHeader );
  1284.         targetItem->bExistsInDocument = false;
  1285.         targetItem->tempFilePtr = sourceItem->tempFilePtr;
  1286.         IncrementTempUsageCount ( targetItem );
  1287.     }
  1288.     
  1289.     HSetState ( (Handle) sourceHeader, sourceState );
  1290.     HSetState ( (Handle) sourceHeader, targetState );
  1291.     
  1292.     return theErr;
  1293. }
  1294.  
  1295.  
  1296.  
  1297. tItemPtr GetNthWindowItem ( WindowRef theWindow, int16 theIndex )
  1298. {
  1299.     tWindowInfoPtr    theInfo = nil;
  1300.     
  1301.     
  1302.     theIndex = GetIndexFromNthWindowItem ( theWindow, theIndex );
  1303.     
  1304.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1305.     return GetNthItem ( (tHeaderHan) theInfo->dataHandle, theIndex );
  1306. }
  1307.  
  1308.  
  1309.  
  1310. int16 GetIndexFromNthWindowItem ( WindowRef theWindow, int16 theIndex )
  1311. {
  1312.     int16            theSize;
  1313.     int                i;
  1314.     Cell            theCell = { 0, 0 };
  1315.     tWindowInfoPtr    theInfo = nil;
  1316.     tHeaderHan        theHeader;
  1317.     Str255            theText;
  1318.     
  1319.     
  1320.     theInfo = (tWindowInfoPtr) GetWRefCon ( theWindow );
  1321.     
  1322.     // First, get the text we're looking for. Two fragments with the same name
  1323.     // but different architectures are quite likely. We need to include the type.
  1324.     
  1325.     theSize = sizeof ( Str255 );
  1326.     theCell.v = theIndex;
  1327.     LGetCell ( &theText[1], &theSize, theCell, theInfo->listRef );
  1328.     theText[0] = theSize;
  1329.     
  1330.     theHeader = (tHeaderHan) theInfo->dataHandle;
  1331.     // Now, scan our items looking for a match
  1332.     for ( i = 0; i < GetItemCount ( theHeader ); i++ )
  1333.     {
  1334.         tItemPtr        theItem;
  1335.         unsigned char    archTypeStr[5];
  1336.         Str255            itemText;
  1337.         
  1338.         theItem = GetNthItem ( theHeader, i );
  1339.         // Since we're looking for an item in the window, ignore deleted items
  1340.         if ( theItem->bDeleted )
  1341.             continue;
  1342.         
  1343.         CopyPStr ( theItem->name, itemText, sizeof ( Str255 ) );
  1344.         ConcatPStr ( itemText, "\p\t", sizeof ( Str255 ) );
  1345.         OSTypeToPStr ( theItem->archType, archTypeStr );
  1346.         ConcatPStr ( itemText, archTypeStr, sizeof ( Str255 ) );
  1347.         if ( EqualString ( theText, itemText, true, true ) )
  1348.             return i;
  1349.     }
  1350.     
  1351.     
  1352.     #if DEBUGGING
  1353.     DebugStr ( "\p GetIndexFromNthWindowItem returning 0" );
  1354.     #endif
  1355.  
  1356.     return 0;
  1357. }
  1358.  
  1359.  
  1360.  
  1361. void AddFragToList ( ListRef theList, tItemPtr theItem )
  1362. {
  1363.     unsigned char    archTypeStr[5];
  1364.     Str255            theText;
  1365.     
  1366.     
  1367.     CopyPStr ( theItem->name, theText, sizeof ( Str255 ) );
  1368.     ConcatPStr ( theText, "\p\t", sizeof ( Str255 ) );
  1369.     OSTypeToPStr ( theItem->archType, archTypeStr );
  1370.     ConcatPStr ( theText, archTypeStr, sizeof ( Str255 ) );
  1371.     AddToList ( theList, theText );
  1372.     
  1373.     return;
  1374. }
  1375.  
  1376.  
  1377.  
  1378. void UpdateFragInList ( WindowRef theWindow, int16 theIndex, StringPtr newName )
  1379. {
  1380.     unsigned char    archTypeStr[5];
  1381.     tItemPtr        theItem;
  1382.     ListRef            theList;
  1383.     Str255            theText;
  1384.     
  1385.     
  1386.     theItem = GetNthWindowItem ( theWindow, theIndex );
  1387.     theList = GetWListRef ( theWindow );
  1388.     CopyPStr ( newName, theText, sizeof ( Str255 ) );
  1389.     ConcatPStr ( theText, "\p\t", sizeof ( Str255 ) );
  1390.     OSTypeToPStr ( theItem->archType, archTypeStr );
  1391.     ConcatPStr ( theText, archTypeStr, sizeof ( Str255 ) );
  1392.     UpdateList ( theList, theIndex, theText );
  1393.     
  1394.     return;
  1395. }
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.